package com.sgzn.sgpay.web.controller.check;


import com.alibaba.fastjson.JSON;
import com.sgzn.sgpay.enums.AppName;
import com.sgzn.sgpay.model.request.TcpTestRequest;
import com.sgzn.sgpay.model.response.SgsResultResponse;
import com.sgzn.sgpay.service.SgsTestService;
import com.sgzn.sgpay.util.SerialNoUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 检查Service是否启动成功
 * @author huangsenguang
 */
@Slf4j
@Controller
public class ServiceCheckOKController {

    @Resource
    private SgsTestService sgsTestService;

    @RequestMapping("/test/service")
    @ResponseBody
    public String testServcie(@Param("testFlag") String testFlag){
        String initiationID = SerialNoUtil.generateSerialNo(AppName.SGW);
        TcpTestRequest request = assembleRequest(initiationID, testFlag);
        SgsResultResponse<String> response = this.sgsTestService.invoke(request);
        return JSON.toJSONString(response)+"<br/><h2>Hello, Sgpay-service start success<h2>";
    }

    private TcpTestRequest assembleRequest(String initiationID, String testFlag) {
        TcpTestRequest testRequest = new TcpTestRequest();
        testRequest.setInitiationID(initiationID);
        testRequest.setReqTime(new Date());
        testRequest.setLoginChannel(AppName.SGW.getNumber());
        testRequest.setTestFlag(testFlag);
        return testRequest;
    }

}

